package de.xt.logging.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class VBSUtils {

	private VBSUtils() {
	}

	public static boolean isRunning(boolean isWindows) {
		if (isWindows) {
			boolean found = false;
			try {
				File file = File.createTempFile("realhowto", ".vbs");
				file.deleteOnExit();
				FileWriter fw = new java.io.FileWriter(file);

				String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
						+ "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
						+ "Set service = locator.ConnectServer()\n"
						+ "Set processes = service.ExecQuery _\n"
						+ " (\"select * from Win32_Process where name='"
						+ "nwn2main.exe"
						+ "'\")\n"
						+ "For Each process in processes\n"
						+ "wscript.echo process.Name \n"
						+ "Next\n"
						+ "Set WSHShell = Nothing\n";

				fw.write(vbs);
				fw.close();
				Process p = Runtime.getRuntime().exec(
						"cscript //NoLogo " + file.getPath());
				BufferedReader input = new BufferedReader(
						new InputStreamReader(p.getInputStream()));
				String line;
				line = input.readLine();
				if (line != null) {
					if (line.equals("nwn2main.exe")) {
						found = true;
					}
				}
				input.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
			return found;
		}else{
			String command="pgrep -l Neverwinter";
			try {
			      String line;
			      Process p = Runtime.getRuntime().exec(command);
			      BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
			      while ((line = input.readLine()) != null) {
			        System.out.println(line);
			      }
			      input.close();
			      return false;
			    }catch (Exception err) {
			      err.printStackTrace();
			    }
		}
		return false;
	}


	public static void msgBox(String msg) {
		javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
				msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
	}
}
